<?php
/**
* 
*/
class upload 
{
	

	function uploadImage($postname){

		// 设置默认区时
		date_default_timezone_set("Asia/Shanghai");
		//获取表单提交过来的数据，file为缩略图
		$fileInfo = $postname;
		$filename = $fileInfo['name'];
		$type = $fileInfo['type'];
		$tmp_name = $fileInfo['tmp_name'];
		$size = $fileInfo['size'];
		$error = $fileInfo['error'];

		//定义上传文件的存放路径
		$upload_path = iconv("UTF-8", "GBK", "upload/images/");
		if (!file_exists($upload_path)){
			mkdir ($upload_path,0777,true);
			// echo '创建文件夹成功';
		}
		//定义允许上传的图片类型
		$allow_type = array('image/jpg','image/jpeg','image/gif','image/png','image/x-png','image/pjpeg');
		// 允许上传的图片后缀
		$allowedExts = array("gif", "jpeg", "jpg", "png");
		//判断文件类型是否被允许上传
		if(!in_array($type, $allow_type)){
			$result = [
		    	'code' => 0,
		    	'msg' => '只允许上传图片格式文件！'
		    ];
		    return $result;
		  	die();
		}

		//判断文件上传是否过大// 小于 1MB
		if (!($size < 10240000)) {
			$result = [
		    	'code' => 0,
		    	'msg' => '图片大于1Mb请压缩后再上传'
		    ];
		    return $result;	
			die();
		}

		//判断是否是通过表单POST上传
		if(!is_uploaded_file($tmp_name)){
		  return ;
		}

		//通过反转及分割获取图片的后缀
		$string = strrev($filename);
		$array = explode('.',$string);
		$ex = strrev($array[0]);

		//判断文件格式
		if (!in_array($ex, $allowedExts)) {
			$result = [
		    	'code' => 0,
		    	'msg' => '非法的文件格式'
		    ];
		    return $result;	
			die();
		}
		//将上传图片重命名为日期加100-999的随机数并加上后缀名
		// $rename = date("Y").date("m").date("d").date("H").date("i").date("s").'_'.rand(100, 999).".".$ex;
		$rename = date("Y").date("m").date("d").date("H").date("i").date("s").'_'.md5($filename).".".$ex;
		//获取上传文件夹所有图片文件
		$imageNames =$this->getDir($upload_path);
		foreach ($imageNames as $value) {
			$imageName = $this->getNeedBetween($value,'_','.');//获取（时间）_和.jpg之间的字符串
			if (md5($filename) == $imageName) {
				if (filesize($upload_path . $value) == $size) {
					$result = [
				    	'code' => 0,
				    	'msg' => $filename . '文件已存在，请勿重新上传'
				    ];
				    return $result;	
					die();
				}		
			}
		}
		// 判断当期目录下的 upload 目录是否存在该文件
		// 如果没有 upload 目录，你需要创建它，upload 目录权限为 777
		if (file_exists($upload_path. $rename)){
			$result = [
		    	'code' => 0,
		    	'msg' => $filename . " 文件已经存在。 "
		    ];
		    return $result;		   
		    die();
		}else{
		    // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
		    move_uploaded_file($tmp_name, $upload_path. $rename);
		    // echo "文件存储在: " . $upload_path . $rename;
		    $result = [
		    	'code' => 1,
		    	'msg' => '文件上传成功！',
		    	'imgUrl' => 'http://'.$_SERVER["SERVER_NAME"].dirname($_SERVER["PHP_SELF"]).'/'.$upload_path. $rename
		    ];
		    return $result;
		    die();
		}

	}
	

	//获取图片
	function searchDir($path,&$data){
	    if(is_dir($path)){
	        $dp=dir($path);
	        while($file=$dp->read()){
	            if($file!='.'&& $file!='..'){
	                $this->searchDir($path.'\\'.$file,$data);
	            }
	        }
	    $dp->close();
	    }
	    if(is_file($path) && preg_match( "/\.(gif|jpeg|jpg|png|bmp)$/i" , $path ) ){
	        $data[]=basename($path);
	        // $data[]=basename($path,".jpg");
	    }
	}
	//获取文件名称
	function getDir($dir){
	    $data=array();
	    $this->searchDir($dir,$data);
	    return   $data;
	}
	//获取两个特定符号中间的字符串
	function getNeedBetween($kw1,$mark1,$mark2){
		$kw=$kw1;
		$kw='123'.$kw.'123';
		$st =stripos($kw,$mark1);
		$ed =stripos($kw,$mark2);
		if(($st==false||$ed==false)||$st>=$ed)
		return 0;
		$kw=substr($kw,($st+1),($ed-$st-1));
		return $kw;
	}

}
?>